<?php
defined('IN_YuLin') || exit('NO PERMIT!'); 
$page = 'statistic/user';

$profitTab = Table('user_lottery_profit');
$sql       = 'SELECT fit.`bet_amount`, fit.`prize_amount`, fit.`winner_amount`, fit.`datetime`, u.`nickname`, u.`username`, u.`money` as user_money FROM 
                yulin_user_lottery_profit AS fit
                LEFT JOIN yulin_user AS u
                ON fit.`uid` = u.`id`
            ';
$where     = ' WHERE 1 ';
$ord       = '';
$searchBtime = $_GET['btime'];
$searchEtime = $_GET['etime'];

$username  = NoBadStr($_GET['username']);
$btime     = NoBadStr($searchBtime);
$etime     = NoBadStr($searchEtime);
$orderby   = NoBadStr($_GET['orderby']);
switch ($orderby){
    case 'bet_asc':
        $ord = ' ORDER BY fit.bet_amount ASC';
        break;
    case 'bet_desc':
        $ord = ' ORDER BY fit.bet_amount DESC';
        break;
    case 'prize_asc':
        $ord = ' ORDER BY fit.prize_amount ASC';
        break;
    case 'prize_desc':
        $ord = ' ORDER BY fit.prize_amount ASC';
        break;
    default :
        $ord = ' ORDER BY fit.datetime DESC';
        break;
}
if ($username) $where .= ' AND u.username="' . $username . '"';
if ($btime)    $where .= ' AND fit.datetime >= ' . strtotime($btime);
if ($etime)    $where .= ' AND fit.datetime <= ' . strtotime($etime);
$sql .= $where . $ord;
$tmp = $db->getrow($sql);
if(count($tmp) > 0){
    $nums     = max(1,intval($_GET['page']));
    $size     = 20;
    $limit    = ' LIMIT '.($nums - 1) * $size.','.$size;
    $url      = '?m=' . $m . '&c=' . $c . '&username=' . $username  .'&btime='. $btime . '&etime=' . $etime . '&orderby' . $orderby;
    $data     = $db->getall($sql . $limit);
    $multi    = Multi($url, $nums, $tmp['p'], $size);
}
foreach ($data as $k => $v){
    $data[$k]['shenglv']     = round($v['prize_amount'] / $v['bet_amount'] , 2); 
    $data[$k]['re_datetime'] = date('Y-m-d', $v['datetime']);
}
$tpl->display($page);
